/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.devs.benchmark.topology;

import java.util.List;
import org.devs.benchmark.Util;
import org.devs.benchmark.modeling.CoupledType;
import org.devs.benchmark.modeling.ModelType;

/**
 *
 * @author Alejandro Moreno
 */
public class Star extends Network{
    int perimeter;


    public Star(String name, int perimeter, Class message,int recursion) throws Exception {
        super(name, message, recursion);
        this.perimeter = perimeter;

        if (perimeter < 1 || recursion <1) {
            throw new Exception("Parameters are incorrect");
        }


    }

   

    public Star(String name, int perimeter, Class message) throws Exception {
        super(name, message);

        this.perimeter = perimeter;

        if (perimeter < 1) {
            throw new Exception("Parameters are incorrect");
        }

        

    }

    public int getPerimeter() {
        return perimeter;
    }

    public void setPerimeter(int perimeter) {
        this.perimeter = perimeter;
    }



   public void buildModel(){
       super.setModel(this.createStarNetwork(this.name, this.perimeter, super.message, this.recursion));
   }


    public void connectStarNodes(CoupledType coupled) {


        if (coupled.getModels().size() > 0) {

            List<ModelType> models = coupled.getModels();

            addConnectionToParent(coupled, coupled.getModels().size()-1, coupled.getModels().size()-1, 0);
            addConnectionFromParent(coupled, coupled.getModels().size()-1, coupled.getModels().size()-1, 0);
            

            for (int i = 0; i < (models.size()-1); i++) {

                addConnection(coupled, i, 0, models.size()-1, i);
                addConnection(coupled, models.size()-1, i, i, 0);
                

            }


        }

    }

    public ModelType createStarNetwork(String name, int length, Class message, int recursion) {
        return createStar(name, length,message, "", recursion);
    }

    

    public ModelType createStar(String name, int length, Class message, String id, int recursion) {

        CoupledType coupled = null;

        if (recursion > 0) {

            coupled = new CoupledType(name + "_Coupled_" + id, Util.createInports(1, message), Util.createOutports(1, message));


            for (int j = 0; j < length; j++) {

                coupled.getModels().add(createStar(name, length, message,id + "_" + j, recursion - 1));

            }

            CoupledType center = buildNode(name + "_Center",length+1,message,id);
            coupled.getModels().add(center);
            connectStarNodes(coupled);

        } else {


            coupled = buildNode(name, 1,message,id);


        }

        return coupled;

    }

   
}
